package likou;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/09/22 12:30
 */
public class _725 {
    public static void main(String[] args) {
        _725 one = new _725();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        head.next.next.next.next.next = new ListNode(6);
        head.next.next.next.next.next.next = new ListNode(7);
        head.next.next.next.next.next.next.next = new ListNode(8);
        head.next.next.next.next.next.next.next.next = new ListNode(9);
        head.next.next.next.next.next.next.next.next.next = new ListNode(10);
        one.splitListToParts(head, 3);
    }

    public ListNode[] splitListToParts(ListNode head, int k) {
        ListNode[] res = new ListNode[k];
        ListNode deep = head;
        int n = 0;
        while (deep != null) {
            n++;
            deep = deep.next;
        }
        int part = n / k;
        int p = n % k;
        deep = head;
        for (int i = 0; i < k; i++) {
            res[i] = deep;
            for (int j = 0; j < part - 1; j++) {
                deep = deep.next;
            }
            if(p>0&&part>0){
                deep = deep.next;
                p--;
            }
            if (deep != null) {
                ListNode temp = deep.next;
                deep.next = null;
                deep = temp;
            }
        }
        return res;
    }
}
